package com.geekaca.testthread.work;

public class Test1 {

//    static int number = 2000;

    public static void main(String[] args) {

        Test2 test2 = new Test2();
        Test3 test3 = new Test3();
        Thread t1 = new MyThread(test2, "thread_1:");
        Thread t2 = new MyThread(test2, "thread_2:");
        Thread t3 = new MyThread(test2, "thread_3:");
        Thread t4 = new MyThread(test2, "thread_4:");
        Thread t5 = new MyThread(test2, "thread_5:");
        Thread t6 = new MyThread(test2, "thread_6:");
        t1.start();
        t2.start();
        t3.start();
        t4.start();
        t5.start();
        t6.start();
    }
}

class MyThread extends Thread {
    private final Test2 test2;

    public MyThread(Test2 test2, String name) {
        this.setName(name);
        this.test2 = test2;
    }

    @Override
    public void run() {
        while (true) {
            synchronized (this.test2) {
                if(this.test2.number==0){
                    break;
                }
                reduceNumber(this.test2);

            }
        }
    }

    private void reduceNumber(Test2 test2) {
        test2.number -= 1;
        System.out.println(this.getName() + " number: " + test2.number);

    }
}

class Test2 {
    int number = 2000;

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }
}

class Test3 {

}




